﻿using A.M.ETO;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.Repository;
using L.M.System.Read.API.MD5Infor;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Public_Class_Library;
using System.Security.Cryptography;

namespace L.M.System.Read.API.Controllers
{
    /// <summary>
    /// 员工
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Staff_tableReadController : ControllerBase
    {
        private readonly IStaff_tableServices staff_TableServices;
        public Staff_tableReadController(IStaff_tableServices staff_TableServices)
        {
            this.staff_TableServices = staff_TableServices;
        }

        /// <summary>
        /// 员工分页查询员工 + 分页
        /// </summary>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="tName">查询条件</param>
        /// <returns>分页查询员工 + 分页</returns>
        [HttpGet]
        public async Task<IActionResult> Getstaff_tableShow(int pageSize, int pageIndex, string? tName)
        {
            return  Ok(await staff_TableServices.Getstaff_tableShow(pageSize, pageIndex, tName));
        }


        /// <summary>
        /// 员工信息添加
        /// </summary>
        /// <param name="addDTO">实体</param>
        /// <returns>员工信息添加</returns>
        [HttpPost]
        public async Task<IActionResult> SaffAddInfor(StaffAddDTO addDTO)
        {
            addDTO.employee_cipher = GetMd5Infor.Md5LogInfor(addDTO.employee_cipher);
            return Ok(await staff_TableServices.SaffAddInfor(addDTO));
        }

        /// <summary>
        /// 上传文件到七牛云
        /// </summary>
        /// <param name="file">文件</param>
        /// <returns>是否成功</returns>
        [HttpPost]
        public IActionResult AddFileInfor(IFormFile file)
        {
            var res = FileHelper.UploadFile(file);
            return Ok(file.FileName);
        }

        /// <summary>
        /// 获取所有的用户
        /// </summary>
        /// <returns>获取所有的用户</returns>
        [HttpGet]
        public  async Task<IActionResult> GetAllShow()
        {
            return Ok(await staff_TableServices.GetShowAllInfor());
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInforDTO">修改用户信息</param>
        /// <returns>修改用户信息</returns>
        [HttpPost]
        public async Task<IActionResult> UpdStainfor(UserInforDTO userInforDTO)
        {
            return Ok(await staff_TableServices.UpdStainfor(userInforDTO));
        }

        /// <summary>
        /// 修稿密码
        /// </summary>
        /// <param name="passwordDTO">密码</param>
        /// <returns>修稿密码</returns>
        [HttpPost]
        public async Task<IActionResult> UpdPassWord(PasswordDTO passwordDTO)
        {
            passwordDTO.passwordinfor = GetMd5Infor.Md5LogInfor(passwordDTO.passwordinfor);
            passwordDTO.IsPasswordNew = GetMd5Infor.Md5LogInfor(passwordDTO.IsPasswordNew);
            passwordDTO.PasswordNew = GetMd5Infor.Md5LogInfor(passwordDTO.PasswordNew);
            return Ok(await staff_TableServices.UpdPassWord(passwordDTO));
        }


        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="staffUpdDTO">修改DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPut]
        public async Task<IActionResult> StaffUpdInfor(StaffUpdDTO staffUpdDTO)
        {
            return Ok(await staff_TableServices.StaffUpdInfor(staffUpdDTO));
        }



        /// <summary>
        /// 反填用户信息
        /// </summary>
        /// <param name="sId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> FanInforStaff(string? sId)
        {
            StaffUpdDTO staffUpdDTO = new StaffUpdDTO();
            var res = await staff_TableServices.GetShowObjAsync(c => c.employee_number == sId);
            if (res != null)
            {
                staffUpdDTO.employee_number = res.employee_number;
                staffUpdDTO.employee_name = res.employee_name;
                staffUpdDTO.department_number = res.department_number;
                staffUpdDTO.department_remark = res.department_remark;
                staffUpdDTO.department_tel = res.department_tel;
            }
            return Ok(staffUpdDTO);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="getUserInfor">User</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> GetInforLogin(GetUserInfor getUserInfor)
        {
            getUserInfor.employee_cipher = GetMd5Infor.Md5LogInfor(getUserInfor.employee_cipher);
            return Ok(await staff_TableServices.GetInforLogin(getUserInfor));
        }


        /// <summary>
        /// 生成一个验证码
        /// </summary>
        /// <returns>生成一个验证码</returns>
        [HttpGet]
        public IActionResult Verification_code()
        {
            return Ok(staff_TableServices.Verification_code());
        }

        /// <summary>
        /// 验证码登录
        /// </summary>
        /// <param name="uTel">手机号</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> TelInforLogin(string? uTel, string? code)
        {
            return Ok(await staff_TableServices.TelInforLogin(uTel, code));
        }


        /// <summary>
        /// 密码验证码验证
        /// </summary>
        /// <param name="sId"></param>
        /// <param name="yId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVerification(string? sId, string? yId)
        {
            return Ok(staff_TableServices.GetVerification(sId, yId));
        }

        /// <summary>
        /// 获取到手机号验证登录码
        /// </summary>
        /// <param name="uTel"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTelLogin(string? uTel)
        {
            return Ok(staff_TableServices.GetTelLogin(uTel));
        }


        /// <summary>
        /// 修改用户登录状态
        /// </summary>
        /// <param name="uId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetUpdStaty(string? uId)
        {
            return Ok(await staff_TableServices.GetUpdStaty(uId)); 
        }
    }
}
